import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.*;

public class FinalProject extends Application {
    
    //FlowPane root = new FlowPane();
    GridPane root = new GridPane();
    Label fileNameLabel = new Label("File Name:");
    Label endsLabel = new Label("Ends:");
    Label summaryLabel = new Label("Summary:");
    Label dateLabel = new Label("Date:");
    Label fromLabel = new Label("From");
    Label toLabel = new Label("To");
    Label locationNameLabel = new Label("Location Name:");
    Label geographicPositionLabel = new Label("Geographic Position:");
    Label latitudeLabel = new Label("Latitude");
    Label longitudeLabel = new Label("Longitude");
    Label timezoneLabel = new Label("Timezone:");
    Label privacyLabel = new Label("Privacy:");
    Label priorityLabel = new Label("Priority:");
    Label repeatsLabel = new Label("Repeats:");
    Label repeatEveryLabel = new Label("Repeats every:");
    Label repeatEveryLabel2 = new Label("");
    Label repeatYesNo = new Label("Turn repeat on?");
    TextField fileNameTextField = new TextField("Event");
    TextField fromDateTextField = new TextField("MM/DD/YYYY");
    TextField fromTimeTextField = new TextField("HH:MM");
    TextField toDateTextField = new TextField("MM/DD/YYYY");
    TextField toTimeTextField = new TextField("HH:MM");
    TextField numbersTextField = new TextField("");
    TextField summaryTextField = new TextField();
    TextField locationNameTextField = new TextField();
    TextField latitudeTextField = new TextField();
    TextField longitudeTextField = new TextField();
    TextField dateTextField = new TextField("MM/DD/YYYY");
    TextField occurancesTextField = new TextField("# Occurances");
    Button repeatButton = new Button("Repeat");
    Button saveButton = new Button("Save File");
    Button doneButton = new Button("Done");
    Button cancelButton = new Button("Cancel");
    ToggleGroup group = new ToggleGroup();
    ToggleGroup yesOrNo = new ToggleGroup();
    RadioButton neverRb = new RadioButton("Never");
    RadioButton afterRb = new RadioButton("After");  
    RadioButton onRb = new RadioButton("On");
    RadioButton yesRb = new RadioButton("Yes");
    RadioButton noRb = new RadioButton("No");
    Stage repeatStage = new Stage(); 
    String repeats, repeatEvery, occurances, dateStopRepeat;
    StoreInput data  = new StoreInput();
    Scene scene = new Scene(root, 750, 500);
    
    ObservableList<String> fromTimeOptions = FXCollections.observableArrayList("AM", "PM");
    ComboBox fromTimeList = new ComboBox(fromTimeOptions);
    
    ObservableList<String> toTimeOptions = FXCollections.observableArrayList("AM", "PM");
    ComboBox toTimeList = new ComboBox(toTimeOptions);
    
    ObservableList<String> timezoneOptions = FXCollections.observableArrayList("(UTC-8:00) Pacific Time", "(UTC-7:00) Mountain Time", "(UTC-6:00) Central Time", "(UTC-5:00) Eastern Time", "(UTC-4:00) Atlantic Time");
    ComboBox timezoneList = new ComboBox(timezoneOptions);
    
    ObservableList<String> privacyOptions = FXCollections.observableArrayList("Default", "Public", "Private", "Confidential     ");
    ComboBox privacyList = new ComboBox(privacyOptions);
    
    ObservableList<String> priorityOptions = FXCollections.observableArrayList("Low", "Medium", "High");
    ComboBox priorityList = new ComboBox(priorityOptions);
    
    ObservableList<String> repeatsOptions = FXCollections.observableArrayList("Daily", "Weekdays", "Weekly", "Monthly", "Yearly");
    ComboBox repeatsList = new ComboBox(repeatsOptions);
    
    ObservableList<String> repeatEveryOptions = FXCollections.observableArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
    ComboBox repeatEveryList = new ComboBox(repeatEveryOptions);
    
    ObservableList<String> options3 = FXCollections.observableArrayList("Days", "Weeks", "Months", "Years");
    ComboBox repeatsList3 = new ComboBox(options3);
    
    public static void main(String[] args) 
    {FinalProject.launch();}  
    @Override
    
    public void start(Stage primaryStage) {
        root.setAlignment(Pos.TOP_LEFT);
        root.setHgap(10);
        root.setVgap(10);
        root.add(fileNameLabel, 1, 1);
        root.add(fileNameTextField, 2, 1);
        root.add(summaryLabel, 1, 2);
        root.add(summaryTextField, 2, 2);
        root.add(dateLabel, 1, 3);
        root.add(fromLabel, 1, 4);
        root.add(fromDateTextField, 2, 4);
        root.add(fromTimeTextField, 3, 4);
        root.add(fromTimeList, 4, 4);
        root.add(toLabel, 1, 5);
        root.add(toDateTextField, 2, 5);
        root.add(toTimeTextField, 3, 5);
        root.add(toTimeList, 4, 5);
        root.add(repeatButton, 5, 5);
        root.add(locationNameLabel, 1, 6);
        root.add(locationNameTextField, 2, 6);
        root.add(geographicPositionLabel, 1, 7);
        root.add(latitudeLabel, 1, 8);
        root.add(latitudeTextField, 2, 8);
        root.add(longitudeLabel, 1, 9);
        root.add(longitudeTextField, 2, 9);
        root.add(privacyLabel, 1, 10);
        root.add(privacyList, 2, 10);
        root.add(priorityLabel, 1, 11);
        root.add(priorityList, 2, 11);
        root.add(saveButton, 5, 11);

        fromTimeList.setValue("AM");
        toTimeList.setValue("AM");
        timezoneList.setValue("(UTC-8:00) Pacific Time");
        privacyList.setValue("Default");
        priorityList.setValue("Low");
        
        primaryStage.setTitle("Final Project");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        repeatButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent){
                //FlowPane root = new FlowPane();
                GridPane root = new GridPane();
                Scene repeatScene = new Scene(root, 500, 400);
            
                root.setAlignment(Pos.TOP_LEFT);
                root.setHgap(10);
                root.setVgap(10);
                root.add(repeatsLabel, 1, 1);
                root.add(repeatsList, 2, 1);
                root.add(repeatEveryLabel, 1, 2);
                root.add(numbersTextField, 2, 2);
                root.add(repeatEveryLabel2, 3, 2);
                root.add(endsLabel, 1, 3);
                root.add(neverRb, 1, 4);
                root.add(afterRb, 1, 5);
                root.add(occurancesTextField, 2, 5);
                root.add(onRb, 1, 6);
                root.add(dateTextField, 2, 6);
                root.add(repeatYesNo, 1, 7);
                root.add(yesRb, 1, 8);
                root.add(noRb, 2, 8);
                root.add(doneButton, 3, 8);
                root.add(cancelButton, 4, 8);
                //root.getChildren().addAll(repeatsLabel, repeatsList, repeatEveryLabel, repeatEveryList, repeatsList3, endsLabel, neverRb, afterRb, occurancesTextField, onRb, dateTextField, doneButton, cancelButton);

                repeatsList.valueProperty().addListener(new ChangeListener<String>() {
                        public void changed(ObservableValue ov, String t, String t1){
                                //root.getChildren().remove(repeatEveryLabel2);
                                switch (t1) {
                                //case "None":    repeatEveryLabel2.setText(" ");   break;
                                case "Daily":    repeatEveryLabel2.setText("days");   break;
                                case "Weekdays": repeatEveryLabel2.setText("days");   break;
                                case "Weekly":   repeatEveryLabel2.setText("weeks");  break;
                                case "Monthly":  repeatEveryLabel2.setText("months"); break;
                                case "Yearly":   repeatEveryLabel2.setText("years");  break;
                                }
                                //root.getChildren().add(repeatEveryLabel2);
                        }
                });
                
                neverRb.setToggleGroup(group);
                neverRb.setSelected(true);
                neverRb.setUserData("Never");
                afterRb.setToggleGroup(group);
                afterRb.setUserData("After");
                onRb.setToggleGroup(group);
                onRb.setUserData("On");
                
                yesRb.setToggleGroup(yesOrNo);
                yesRb.setSelected(true);
                yesRb.setUserData("Yes");
                noRb.setToggleGroup(yesOrNo);
                noRb.setUserData("No");
                
                repeatsList.setValue("Daily");
                repeatEveryList.setValue("1");
                repeatsList3.setValue("Days");
                
                repeatStage.setTitle("Repeat");
                repeatStage.setScene(repeatScene);
                repeatStage.show();
                }
                
        });
       
        saveButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent){
                        
                        //Store user inputs
                        data.setFileName(fileNameTextField.getText());
                        data.setSummary(summaryTextField.getText());
                        data.setFromDate(fromDateTextField.getText());
                        data.setFromTime(fromTimeTextField.getText() + " " + fromTimeList.getValue().toString());
                        data.setToDate(toDateTextField.getText());
                        data.setToTime(toTimeTextField.getText() + " " + toTimeList.getValue().toString());
                        data.setTimezone(timezoneList.getValue().toString());
                        data.setLocation(locationNameTextField.getText());
                        data.setLatitude(latitudeTextField.getText());
                        data.setLongitude(longitudeTextField.getText());
                        
                        try {
                        	GoogleResponse res = new AddressConverter().convertToLatLong(data.getLocation());
                            
                            for(Result result : res.getResults()) {
                            	data.setLatitude(result.getGeometry().getLocation().getLat());
                            	data.setLongitude(result.getGeometry().getLocation().getLng());
                            }
                            latitudeTextField.setText(data.getLatitude());
                            longitudeTextField.setText(data.getLongitude());
                        } catch (Exception e) {
                        	System.out.println("INVALID ADDRESS");
                        }
                        
                        data.setPrivacy(privacyList.getValue().toString());
                        data.setPriority(priorityList.getValue().toString());
                        
                        //Create .ics file
                        CalLogic mycal = new CalLogic();
                        mycal.toICSFile(data);
                }
        });
        
        
        doneButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent){

                        //Store user inputs
                        data.setRepeats(repeatsList.getValue().toString());
                        data.setRepeatEvery(numbersTextField.getText());
                        
                        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
                              public void changed(ObservableValue<? extends Toggle> ov,
                                  Toggle old_toggle, Toggle new_toggle) {
                                if (group.getSelectedToggle() != null) {
                                        data.setRepeatTill(group.getSelectedToggle().getUserData().toString());
                                }
                              }
                        });;
                        
                        if(data.getRepeatTill() == "After") 
                        {
                        	data.setDateStopRepeat("");    
                        	data.setOccurrances(occurancesTextField.getText());
                        }
                        
                        if(data.getRepeatTill() == "On")
                        {
                        	data.setOccurrances("");    
                        	data.setDateStopRepeat(dateTextField.getText());
                        }

                        if(yesOrNo.getSelectedToggle().getUserData().toString() == "No")
                        {
                        	data.setRepeats("");
                        	data.setRepeatEvery("");
                        	data.setOccurrances("");
                        	data.setDateStopRepeat("");
                        	data.setRepeatTill("Never");
                        }
                        
                        if(data.getRepeatTill() == "After")
                                System.out.println("occurances = " + data.getOccurances());
                        if(data.getRepeatTill() == "On")
                                System.out.println("dateStopRepeat = " + data.getDateStopRepeat());

                        repeatStage.close();
                }
        });
        
        cancelButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent)
            {repeatStage.close();}
        });
    }
}